<?php


class ShopAction extends SecurityAction{
	function _initialize(){
        $this->assign("menuFirst",session('menuFirst')); 
        $this->assign("menuSecond",session('menuSecond')); 
    }
		
	function slist(){
		
		if ($_GET['opt'] == 'add'){  //新增跳转
			$this->display('Shop/detail');
			return;
		}


	    if ($_GET["opt"] == 'edit'){   //编辑
	        $ob = M("yq_shop");
	        $info = $ob->where(array("tid"=>$_GET["tid"]))->find();
	        $this->assign("modify", '1');
	        $this->assign("info", $info);

	        $this->display('Shop/detail');
	        return;
	    }

		$ob = M("yq_shop");
        
		if(session('info.roleid')=='1'){
		    //总经理
		    $sql = array();
		}elseif(session('info.roleid')=='5'){
		    //客服
		    $sql = array('roleid'=>'5','tid'=>session('info.tid'));
		}else{
		    $sql = array('roleid'=>'0000000','tid'=>session('info.tid'));
		}
		
		if($_GET['account']){
		    $sql[] = array('account'=>array('like',"%{$_GET['account']}%"));
		}
		
		$cpp = 30; //分页数
        import('ORG.Util.Page');// 导入分页类
        $count      = $ob->where($sql)->count();// 查询满足要求的总记录数
        $Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
        $show       = $Page->show();// 分页显示输出
        
		$list = $ob->where($sql)->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出

		$this->display("Shop/slist");
	}
	

	function detail(){
	    if($_POST){
	        //添加
	        $_POST['adminid'] = session('info.tid');
	        $_POST['add_time'] = time();//date('Y-m-d H:i:s', time());
	        $ob = M("yq_shop");
	    
	        if($_POST['tid'] != ''){
	            $ob->where(array('tid'=>$_POST['tid']))->save($_POST);
	        }else{
	           /* $info = $ob->where(array('username'=>$_POST['username']))->find();
	            if(!empty($info)){
	                $this->error('登录账号重复，请重新添加','/Store/detail');
	                return;
	            }*/
	            $ob->add($_POST);
	        }
	        	
	        $this->redirect('/Shop/slist/');
	        return;
	    }
	  
	    
	}


	//修改当前登录人密码
	public function updatePass(){
		//修改密码
		if ($_POST){
			$ob = M("csl_user");

			$_POST["password"] = md5($_POST["password"].session('info.salt'));

			$ob->where(array("uid" =>$_POST["uid"]))->save($_POST);
			$this->redirect("/Store/slist");
			return;
		}

		//修改菜单跳转修改页面
		$info["uid"] = session('info.uid');
		$info["username"] = session('info.username');
		$this->assign("info",$info);
		$this->display("/Store/personInfo");
	}
	
	
	
	function stocklist(){
	
		$ob = M("yq_stock");
	
		/* if(session('info.roleid')=='1'){
			//总经理
			$sql = array();
		}elseif(session('info.roleid')=='5'){
			//客服
			$sql = array('roleid'=>'5','tid'=>session('info.tid'));
		}else{
			$sql = array('roleid'=>'0000000','tid'=>session('info.tid'));
		} */
		$keyWord = $_GET["keyWord"];
		
		if($keyWord){
	/* 		$sql[] = array('goods_code'=>array('like',"%".$keyWord."%"));
			$sql['_logic'] = 'or';
			$sql[] = array('goods_name'=>array('like',"%".$keyWord."%")); */
			
			$sql[] = "goods_code like '%$keyWord%' or goods_name like '%$keyWord%'";
		}
	
		if($_GET['shopid']){
			$sql[] = array('shopid'=>$_GET['shopid']);
		}

		$cpp = 300; //分页数
		import('ORG.Util.Page');// 导入分页类
		$count      = $ob->join("yq_shop b on yq_stock.shopid=b.tid")
						 ->join("left join yq_goods c on yq_stock.goodsid = c.tid ")
						 ->where($sql)->count();// 查询满足要求的总记录数
		$Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
	
		$list = $ob->join("yq_shop b on yq_stock.shopid=b.tid")
				   ->join("left join yq_goods c on yq_stock.goodsid = c.tid ")
				   ->field("yq_stock.tid,yq_stock.shopid,yq_stock.goodsid,yq_stock.stock_num,
				   		yq_stock.ctime,b.shopname,
				   		c.goods_code,c.goods_name")
				   ->where($sql)->order(array("shopid"=>"asc"))
				   ->limit($Page->firstRow.','.$Page->listRows)->select();
	
		$shop = M("yq_shop");
		$shoplist = $shop->where(array("is_forbit"=>"1"))->select();
		
		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出
		$this->assign("shoplist", $shoplist);
		
		$this->display("/Shop/stocklist");
	}
	
	function stockdetail(){
		if ($_POST){
			$ob = M("yq_stock");
			if ($_POST['tid'] == ''){
				$info = $ob->where(array("shopid"=>$_POST["shopid"],"goodsid"=>$_POST["goodsid"]))->select();
				if ($info){
					$this->error("该物品已经存在!");
					return;
				}else{
					$_POST["operator_ip"] = $_SERVER["REMOTE_ADDR"];
					$_POST["adminid"] = session("info.tid");
					$ob->add($_POST);
				}
			}else{
				$ob->where(array("tid"=>$_POST["tid"]))->save($_POST);
			}
			$this->redirect("/shop/stocklist");
			return;
		}
		
		if ($_GET['opt'] == 'add'){  //新增跳转
			$ob = M("yq_shop");
			$shoplist = $ob->where(array("is_forbit"=>"1"))->select();
			
			$ob2 = M("yq_goods");
			$goodslist = $ob2->where(array("status"=>"1"))->select();
			
			$this->assign("shoplist", $shoplist);
			$this->assign("goodslist", $goodslist);
		    $this->display('Shop/stockdetail');
			return;
		}
		
		if ($_GET["opt"] == 'edit'){   //编辑
			$ob = M("yq_stock");
			$info = $ob->where(array("tid"=>$_GET["tid"]))->find();
			
			$ob = M("yq_shop");
			$shoplist = $ob->where(array("is_forbit"=>"1"))->select();
				
			$ob2 = M("yq_goods");
			$goodslist = $ob2->where(array("status"=>"1"))->select();
			
			$this->assign("shoplist", $shoplist);
			$this->assign("goodslist", $goodslist);
			$this->assign("info", $info);
			$this->display('/Shop/stockdetail');
			return;
		} 
		
		if ($_GET["opt"] == 'del'){   //删除
			$ob = M("yq_stock");
			$info = $ob->where(array("tid"=>$_GET["tid"]))->delete();
			$this->redirect('/Shop/stocklist');
			return;
		}
		
		if ($_GET["opt"] == 'init'){
			Log::write(">>>>>>>>>>>>>>>>>>start init<<<<<<<<<<<<<<<<<<<<");
			//查出启用的商品			
			$ob2 = M("yq_goods");
			$goodslist = $ob2->where(array("status"=>"1"))->select();
			if ($goodslist){
				foreach ($goodslist as $key => $value){
					Log::write("value===" . $value);
					Log::write("goodsname===" . $value["goods_name"]);
					Log::write("goodsid===" . $value["tid"]);
					
					$ob = M("yq_stock");
					$info = $ob->where(array("goodsid"=>$value["tid"],"shopid"=>$_GET["shopid"]))->find();
					if (!$info){
						$data["shopid"] = $_GET["shopid"];
						$data["goodsid"] = $value["tid"];
						$data["stock_num"] = "0";
						$data["shopid"] = $_GET["shopid"];
						$data["operator_ip"] = $_SERVER["REMOTE_ADDR"];
						$data["adminid"] = session("info.tid");
						Log::write("data===". print_r($data,true));
						$ob->add($data);
					}
				}
				
			}
			Log::write(">>>>>>>>>>>>>>>>>>end init<<<<<<<<<<<<<<<<<<<<");
			$this->redirect('/Shop/stocklist/shopid/'.$_GET["shopid"]);
			return;
		}
	}
	
	/**
	 * 修改库存
	 */
	function updateStock(){
		if ($_POST){
			if ($_POST["tid"] != ""){
				$ob = M("yq_stock");
				
				$info =  $ob->where(array("tid"=>$_POST["tid"]))->find();
				
				$result = $ob->where(array("tid"=>$_POST["tid"]))->save($_POST);
				if ($result){
					//库存修改，记录log
					$db = M("yq_logs");
					$data = array(
							"stockid"=>$_POST["tid"],
							"adminid"=>session("info.tid"),
							"shopid"=>$_POST["shopid"],
							"action"=>"修改物品库存 [成功]",
							"content"=>"库存数量：[".$info['stock_num']."]=>[".$_POST['stock_num']."]",
							"operator_id"=>$_SERVER["REMOTE_ADDR"],
							"type"=>"0");
					$db->add($data);
				}
				
			}
			$this->redirect("/Shop/stocklist");
			return;
		}
		
		$db = M("yq_stock");
		$info =  $db->where(array("tid"=>$_GET["tid"]))->find();
		
		$ob = M("yq_shop");
		$shop = $ob->where(array("tid"=>$info["shopid"]))->find();
			
		$ob2 = M("yq_goods");
		$goods = $ob2->where(array("tid"=>$info["goodsid"]))->find();
		
		$dblog = M("yq_logs");
		$log = $dblog->join("yq_admin b on yq_logs.adminid = b.tid ")
					 ->field("yq_logs.stockid,yq_logs.adminid,yq_logs.action,yq_logs.content,
					 		yq_logs.operator_ip,yq_logs.type,yq_logs.ctime,b.nickname ")
					 ->where(array("stockid"=>$_GET["tid"],"type"=>"0"))
					 ->order(array("yq_logs.ctime desc "))->select();
	
		$this->assign("shop", $shop);
		$this->assign("goods", $goods);
		$this->assign("info", $info);
		$this->assign("log", $log);
		$this->display("/Shop/updateStock");
		return;
	}
	
	
	/**
	 * 库存领用
	 */
	function stockuselist(){
		$ob = M("yq_use");
		
		$keyWord = $_GET["keyWord"];
		
		if($keyWord){
			$sql[] = "c.goods_code like '%$keyWord%' or c.goods_name like '%$keyWord%'";
		}
		
		if($_GET['shopid']){
			$sql[] = array('yq_use.shopid'=>$_GET['shopid']);
		}
		
		$cpp = 300; //分页数
		import('ORG.Util.Page');// 导入分页类
		$count      = $ob->join(" yq_shop b on yq_use.shopid=b.tid")
						 ->join(" yq_goods c on yq_use.goodsid = c.tid ")
						 ->join(" yq_member d on yq_use.memberid = d.tid ")
						 ->join(" yq_admin e on yq_use.adminid = e.tid")
						 ->where($sql)->count();// 查询满足要求的总记录数 
		$Page       = new Page($count,$cpp);// 实例化分页类 传入总记录数和每页显示的记录数
		$show       = $Page->show();// 分页显示输出
		
		$list = $ob->join(" yq_shop b on yq_use.shopid=b.tid")
					 ->join(" yq_goods c on yq_use.goodsid = c.tid ")
					 ->join(" yq_member d on yq_use.memberid = d.tid ")
					 ->join(" yq_admin e on yq_use.adminid = e.tid")
					 ->field("yq_use.tid,yq_use.shopid,yq_use.goodsid,yq_use.quantity,
			   				yq_use.status,yq_use.ctime,b.shopname,c.goods_code,c.goods_name,
					 		d.realname as membername,e.nickname as adminname")
					  ->where($sql)->order(array("yq_use.status"=>"asc"))
					  ->limit($Page->firstRow.','.$Page->listRows)->select();
		
		$shop = M("yq_shop");
		$shoplist = $shop->where(array("is_forbit"=>"1"))->select();
		
		$this->assign("list", $list);
		$this->assign('show',$show);// 赋值分页输出
		$this->assign("shoplist", $shoplist);
		
		$this->display("/Shop/stockuse");
	}
	
	/**
	 * 
	 */
	function stockuse(){
		$ob = M("yq_use");
		
		$info = $ob->where(array("tid"=>$_GET["tid"]))->find();
		if ($info){
			$db = M("yq_stock");
			if ($_GET["opt"] == "sure"){  //确认
				// $stock = $db->where(array("tid"=>$_GET["tid"]))->find();

				// $data = array("stock_num"=>$info["quantity"]+$stock["quantity"],"adminid"=>session("info.tid"));
				$where = array("shopid"=>$info["shopid"],"goodsid"=>$info["goodsid"]);
				
				// $result = $db->where($where)->save($data);
				$result = $db->where($where)->setInc("stock_num", $info["quantity"]);
				if ($result){
					$map["status"] = "1"; 
					$ob->where(array("tid"=>$_GET["tid"]))->save($map);
					
					$this->redirect("/Shop/stockuselist");
				}else{
					$this->error("更新物品库存失败");
					return;
				}
			}else if ($_GET["opt"] == "cancel"){  //取消
				$map["status"] = "2";
				$ob->where(array("tid"=>$_GET["tid"]))->save($map);
				
				$this->redirect("/Shop/stockuselist");
				return;
			}
		}else{
			$this->error("该商品部存在!");
			return;
		}
	}
}

?>